package azku.photoviews.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import azku.MyApplication;
import azku.entitys.EListView;
import azku.photoviews.R;

public class ListViewAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<EListView> mListData;

	private int mDefaultPhotoWidth = 90;
	private int mDefaultPhotoHeight = 120;
	private int mDefaultTitleHeight = 46;
	private int mScreanWidth;
	private int mScreanHeight;
	private int mPhotoWidth;
	private int mPhotoHeight;
	private int mTitleWidth;
	private int mTitleHeight;
	private int mInfoWidth;

	private void InitViewItemSize(Context context) {

		mScreanWidth = MyApplication.GetSceneWidth();
		mScreanHeight = MyApplication.GetSceneHeight();
		mPhotoHeight = (mDefaultPhotoHeight * mScreanHeight / (MyApplication.SceneSizes[0][1]));
		mPhotoWidth = (mDefaultPhotoWidth * mScreanWidth / (MyApplication.SceneSizes[0][0]));
		mTitleHeight = (mDefaultTitleHeight * mScreanHeight / (MyApplication.SceneSizes[0][1]));
		mTitleWidth = ((mScreanWidth - mDefaultPhotoWidth) / 2);
		mInfoWidth = mScreanWidth - mPhotoWidth - 10;
	}

	public ListViewAdapter(Context context, List<EListView> listdata) {
		this.mInflater = LayoutInflater.from(context);
		this.mListData = listdata;
		InitViewItemSize(context);
	}

	public void ChangeBind(List<EListView> listData) {
		this.mListData = listData;
	}

	public int getCount() {
		return mListData.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.viewchilditem2, null);
			holder.img = (ImageView) convertView
					.findViewById(R.id.viewchild2_photo);
			holder.title = (TextView) convertView
					.findViewById(R.id.viewchild2_title);
			holder.date = (TextView) convertView
					.findViewById(R.id.viewchild2_data);
			holder.info = (TextView) convertView
					.findViewById(R.id.viewchild2_info);
			holder.rating = (RatingBar) convertView
					.findViewById(R.id.viewchild2_ratingbar);

			holder.img.getLayoutParams().height = mDefaultPhotoHeight;
			holder.img.getLayoutParams().width = mDefaultPhotoWidth;
			holder.title.getLayoutParams().width = mTitleWidth + 40;
			holder.date.getLayoutParams().width = mTitleWidth - 40;
			holder.info.getLayoutParams().width = mInfoWidth;
			
			/*
			 * convertView = mInflater.inflate(R.layout.viewchilditem, null);
			 * holder.img = (ImageView) convertView.findViewById(R.id.img);
			 * holder.reply = (TextView) convertView
			 * .findViewById(R.id.viewchild_replaycout); holder.title =
			 * (TextView) convertView .findViewById(R.id.textview_title);
			 * holder.date = (TextView) convertView
			 * .findViewById(R.id.textview_data); holder.info = (TextView)
			 * convertView .findViewById(R.id.textview_info); holder.rating =
			 * (RatingBar) convertView .findViewById(R.id.textview_ratingbar);
			 * holder.img.getLayoutParams().height = mPhotoHeight;
			 * holder.img.getLayoutParams().width = mPhotoWidth;
			 * holder.title.getLayoutParams().height = mTitleHeight; //
			 * holder.title.getLayoutParams().width = mTitleWidth;
			 * holder.date.getLayoutParams().height = mTitleHeight - 100; //
			 * holder.date.getLayoutParams().width = mTitleWidth;
			 * 
			 * holder.info.getLayoutParams().width = mInfoWidth;
			 */
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		EListView item = mListData.get(position);
		if (item != null) {
			holder.img.setTag(item);
			holder.img.setImageBitmap(item.getImage());
			holder.title.setText(item.getTitle());
			holder.date.setText("[" + item.getGroupTypeName() + "] ");
			holder.info.setText("[" + item.getDate() + "] " + item.getDes());
			holder.rating.setRating(item.getScore());

			/*
			 * holder.reply.setText(Integer.toString(item.getAssessCount()));
			 * holder.rating.setRating(item.getScore());
			 */
		}
		return convertView;
	}
}